package fr.zadoka.zadokadna;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.GregorianCalendar;

import fr.zadoka.zadokadna.Util.CalendarUtil;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

public class ZadokaFileManager {
	
	private Context mContext;
	private static final String TAG = "fr.zadoka.zadokadna.ZadokaFileManager";
	

	public ZadokaFileManager(Context context)
	{
		mContext = context;
	}
	
	private String getFileName(GregorianCalendar date)
	{
		return CalendarUtil.GregorianCalendarToString(date);
	}
	
	private void saveOnInternalStorage(GregorianCalendar date, Bitmap imageToSave)
	{	
		File file = new File(mContext.getFilesDir(), getFileName(date));
		
		saveImage(file, imageToSave);
	}
	
	private void saveOnExternalStorage(GregorianCalendar date, Bitmap imageToSave)
	{	
		File file = new File(mContext.getExternalCacheDir(), getFileName(date));       
		saveImage(file, imageToSave);
	}
	
	private void saveImage(File file, Bitmap imageToSave)
	{
		Log.i(TAG, "Saving to " + file.getPath());
		FileOutputStream outStream;
		try {
			outStream = new FileOutputStream(file);
			imageToSave.compress(Bitmap.CompressFormat.PNG, 100, outStream);
	        outStream.flush();
	        outStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public boolean isExternalStorageWritable() {
	    String state = Environment.getExternalStorageState();
	    if (Environment.MEDIA_MOUNTED.equals(state)) {
	        return true;
	    }
	    return false;
	}	
	
	public boolean isExternalStorageReadable() {
	    String state = Environment.getExternalStorageState();
	    if (Environment.MEDIA_MOUNTED.equals(state) ||
	        Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
	        return true;
	    }
	    return false;
	}
	
	
	public void saveImage(GregorianCalendar date, Bitmap imageToSave)
	{
		if (isExternalStorageWritable())
			saveOnExternalStorage(date, imageToSave);
		else
			saveOnInternalStorage(date, imageToSave);
			
	}
	
	public Bitmap loadImage(GregorianCalendar date)
	{
		if (!isExternalStorageReadable())
			return loadImageFromInternalStorage(date);
		else
		{
			Bitmap image = loadImageFromExternalStorage(date);
			if (image == null)
				return loadImageFromInternalStorage(date);
			return image;
		}
	}

	private Bitmap loadImageFromExternalStorage(GregorianCalendar date) {
		// TODO Auto-generated method stub		
		File file = new File(mContext.getExternalCacheDir(), getFileName(date));       
		Log.i(TAG, "loading file "+file.getPath());
		
		return loadImage(file);		
	}

	private Bitmap loadImageFromInternalStorage(GregorianCalendar date) {
		// TODO Auto-generated method stub
		File file = new File(mContext.getFilesDir(), getFileName(date));
		Log.i(TAG, "loading file "+file.getPath());
				
		return loadImage(file);
	}

	private Bitmap loadImage(File file) {
		// TODO Auto-generated method stub
		Log.i(TAG, "loading file "+file.getPath());
		return BitmapFactory.decodeFile(file.getPath());
	}
	
	private void deleteFileFromInternalStorage(GregorianCalendar imageDate)
	{
		File file = new File(mContext.getFilesDir(), getFileName(imageDate));
		
		file.delete();
	}
	
	private void deleteFileFromExternalStorage(GregorianCalendar imageDate)
	{
		File file = new File(mContext.getExternalCacheDir(), getFileName(imageDate));       

		file.delete();
	}
	
	public void deleteImage(GregorianCalendar imageDate)
	{		
		deleteFileFromInternalStorage(imageDate);
		deleteFileFromExternalStorage(imageDate);
	}
	
}
